import sys
sys.path.append("D:\\Day\\p2p")
import pytest
import requests

from api.api_recharge import ApiRecharge
from api.api_register_login import ApiRegisterLogin
from tools import common_assert, GetLog, read_html, read_json

logger = GetLog.get_logger()
class TestRecharge:
    def setup_method(self):
        # 1、获取session对象
        self.session = requests.Session()
        # 2、获取ApiRecharge对象
        self.recharge = ApiRecharge(self.session)
        # 3、获取登录注册接口并调用登录方法
        ApiRegisterLogin(self.session).api_login()
    def teardown_method(self):
        self.session.close()

    # 1、获取充值图片验证码
    @pytest.mark.parametrize("random,expect_code",read_json("recharge.json","img_code"))
    def test01_img_code(self,random,expect_code):
        try:
            resp = self.recharge.api_img_code(random)
            logger.info("充值图片验证码接口测试结果:{}".format(resp.status_code))
            common_assert(resp,expect_code=expect_code)
            logger.info("充值图片验证码接口断言成功,预期结果:{},实际结果:{}".format(expect_code,resp.status_code))
        except Exception as e:
            logger.error(e)
            raise

    # 2、请求后台充值成功

    def test02_recharge(self,expect_text="form",expect_text2="OK"):
        self.recharge.api_img_code(10000)
        try:
            resp = self.recharge.api_recharge()
            logger.info("请求后台充值接口测试结果:{}".format(resp.text))
            common_assert(resp,expect_text=expect_text)
            logger.info("充值图片验证码接口断言成功,预期结果:{},实际结果:{}".format(expect_text, resp.text))
            # 三方充值
            data = read_html(resp)
            resp2 = self.session.post(url = data[0],data=data[1])
            common_assert(resp2, expect_text=expect_text2)
            logger.info("请求后台充值接口测试结果:{}".format(resp2.text))
        except Exception as e:
            logger.error(e)
            raise